package com.whcoding.rabbitmq.util;

import com.rabbitmq.client.Channel;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;

import java.io.IOException;
import java.io.UnsupportedEncodingException;

/**
 * @program: spring-boot-learning
 * @description:
 * @author: whcoding
 * @create: 2022-09-30 10:48
 **/
public class MqHandleUtils {

	private static final Logger logger = LoggerFactory.getLogger(MqHandleUtils.class);

	public MqHandleUtils() {
	}

	/**
	 * 获取消息信息
	 *
	 * @param message
	 * @return
	 */
	public static String getMessageContent(Message message) {
		byte[] body = message.getBody();
		String messageStr = "";

		try {
			messageStr = new String(body, "UTF-8");
			logger.info("接收消息内容：{}", messageStr);
		} catch (UnsupportedEncodingException var4) {
			logger.error("message convert error!", var4);
		}

		return messageStr;
	}


	/**
	 * 执行ACK
	 *
	 * @param channel
	 * @param deliveryTag
	 */
	public static void handleReceiver(Channel channel, long deliveryTag) {
		try {
			channel.basicAck(deliveryTag, true);
		} catch (IOException var6) {
			logger.error("consume confirm error!", var6);

			try {
				channel.basicNack(deliveryTag, false, false);
			} catch (IOException var5) {
				logger.error("basicReject ERROR", var5);
			}
		}
	}


}
